package com.example.mq.consumer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * fanout exchange
 */
@Configuration
public class FanoutConfig {
	//就是靠他
	@Autowired
	RabbitAdmin rabbitAdmin;
	// @Bean
	// public FanoutExchange fanoutExchange(){
	// 	return  new FanoutExchange("spring.fanout",true,false);
	// }
	//
	// @Bean
	// public Queue fanoutQueue1(){
	// 	return  new Queue("spring.queue");
	// }
	//
	// @Bean
	// public Queue fanoutQueue2(){
	// 	return  new Queue("spring.queue2");
	// }
	// @Bean
	// public Binding fanoutBinding(Queue fanoutQueue1,FanoutExchange fanoutExchange){
	// 	return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
	// }
	// @Bean
	// public Binding fanoutBinding2(Queue fanoutQueue2,FanoutExchange fanoutExchange){
	// 	return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
	// }


	@Bean
	public Queue objectQueue(){
		return new Queue("object.queue");
	}


	@Bean
	public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
		RabbitAdmin rabbitAdmin= new RabbitAdmin(connectionFactory);
		// 只有设置为 true，spring 才会加载 RabbitAdmin 这个类
		rabbitAdmin.setAutoStartup(true);
		return rabbitAdmin;
	}

	//创建交换机和对列
	@Bean
	public void createExchangeQueue() {

		rabbitAdmin.declareQueue(objectQueue());
		// rabbitAdmin.declareExchange(fanoutExchange());
		// rabbitAdmin.declareQueue(fanoutQueue1());
		// rabbitAdmin.declareQueue(fanoutQueue2());
	}
}
